package com.github.postsanf.yinian.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.StringUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.LinkedHashMap;

public class ApplyJoinActivity extends BaseActivity implements View.OnClickListener {
    private EditText et_space_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applyjoin);
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.bt_sure).setOnClickListener(this);
        et_space_code = ((EditText) findViewById(R.id.et_space));
        et_space_code.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                if (cmb.getText() != null) {
                    String text = cmb.getText().toString().trim();
                    et_space_code.setText(text);
                    showToast("粘贴成功");
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_sure:
                String groupName = et_space_code.getText().toString().trim();
                if (StringUtils.isEmpty(groupName)) {
                    showToast("请输入圈子名称");
                    return;
                }
                doJoinGroup();
                break;
        }
    }


    private boolean doCheack() {
        String mCode = et_space_code.getText().toString().trim();
        if (mCode.length() <= 0) {
            showToast("请输入正确邀请码");
            return false;
        }
        if (mCode.equals("天使")) {
            intent2Activity(YNWebActivity.class);
            finish();
            return false;
        }
        return true;
    }


    private void doJoinGroup() {
        if (doCheack()) {
            LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
            mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
            mParam.put(CommonConstants.YNS_INVITE_CODE, et_space_code.getText().toString().trim());
            StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynApplyGroup))
                    .setMethod(HttpMethods.Post)
                    .setParamMap(mParam)
                    .setHttpListener(new HttpListener<String>(true, false, true) {
                        @Override
                        public void onSuccess(String s, Response<String> response) {
                            int code = 0;
                            String msg = "";
                            try {
                                JSONTokener jsonParser = new JSONTokener(s);
                                JSONObject res = (JSONObject) jsonParser.nextValue();
                                code = res.getInt("code");
                                msg = res.getString("msg");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            switch (code) {
                                case 0:
                                    showToast(CommonConstants.TIP_SEND_SUCCESS);
                                    finish();
                                    break;
                                case 2000:
                                    // 官方相册现在不加入主页
//                                    YNAlbumResponse resOfficial = gson.fromJson(s, YNAlbumResponse.class);
//                                    if (resOfficial!=null){
//                                        YNAlbum offiGroup = resOfficial.getData().get(0);
//                                        Intent upIntent = new Intent(ACTIONs.actionInvateGroup);
//                                        upIntent.putExtra(ACTIONs.aAlbumItem, offiGroup);
//                                        upIntent.putExtra(ACTIONs.IsAdd, true);
//                                        sendBroadcast(upIntent);
//                                    }
                                    break;
                                default:
                                    showToast(msg);
                                    break;
                            }

                        }

                        @Override
                        public void onFailure(HttpException e, Response<String> response) {
                            showToast(CommonConstants.YN_NET_LINK);
                        }
                    });
            liteHttp.executeAsync(req);
        }
    }

}
